﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace ElfTyping.General
{
    public enum GameKey
    {
        Up = 0,
        Down,
        Left,
        Right,
        Use,
        Escape,
        Last // Should always be last
    }

    public class InputInfo
    {
        private static Dictionary<char, Keys> KeyMap = new Dictionary<char, Keys>() {
            {'A', Keys.A}, 
            {'B', Keys.B},
            {'C', Keys.C}, 
            {'D', Keys.D},
            {'E', Keys.E}, 
            {'F', Keys.F},
            {'G', Keys.G}, 
            {'H', Keys.H},
            {'I', Keys.I}, 
            {'J', Keys.J},
            {'K', Keys.K}, 
            {'L', Keys.L},
            {'M', Keys.M}, 
            {'N', Keys.N},
            {'O', Keys.O}, 
            {'P', Keys.P},
            {'Q', Keys.Q}, 
            {'R', Keys.R},
            {'S', Keys.S}, 
            {'T', Keys.T},
            {'U', Keys.U}, 
            {'V', Keys.V},
            {'W', Keys.W}, 
            {'X', Keys.X},
            {'Y', Keys.Y}, 
            {'Z', Keys.Z},
        };

        private bool[] currentKeys = new bool[(int)GameKey.Last];
        private bool[] previousKeys = new bool[(int)GameKey.Last];
        private bool[] currentLetters = new bool[26];
        private bool[] previousLetters = new bool[26];

        private PlayerIndex playerIndex = PlayerIndex.One;

        public void Tick(float dt)
        {
            previousKeys = (bool[])currentKeys.Clone();
            previousLetters = (bool[])currentLetters.Clone();

            foreach (KeyValuePair<char, Keys> charKeyPair in KeyMap)
            {
                currentLetters[charKeyPair.Key - 'A'] = Keyboard.GetState().IsKeyDown(charKeyPair.Value);
            }

            // Up
            currentKeys[(int)GameKey.Up] =
                GamePad.GetState(playerIndex).ThumbSticks.Left.Y > 0 ||
                GamePad.GetState(playerIndex).DPad.Up == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Up) ||
                Keyboard.GetState().IsKeyDown(Keys.W);

            // Down
            currentKeys[(int)GameKey.Down] =
                GamePad.GetState(playerIndex).ThumbSticks.Left.Y < 0 ||
                GamePad.GetState(playerIndex).DPad.Down == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Down) ||
                Keyboard.GetState().IsKeyDown(Keys.S);

            // Left
            currentKeys[(int)GameKey.Left] =
                GamePad.GetState(playerIndex).ThumbSticks.Left.X < 0 ||
                GamePad.GetState(playerIndex).DPad.Left == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Left) ||
                Keyboard.GetState().IsKeyDown(Keys.A);

            // Right
            currentKeys[(int)GameKey.Right] =
                GamePad.GetState(playerIndex).ThumbSticks.Left.X > 0 ||
                GamePad.GetState(playerIndex).DPad.Right == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Right) ||
                Keyboard.GetState().IsKeyDown(Keys.D);

            // Use
            currentKeys[(int)GameKey.Use] = Keyboard.GetState().IsKeyDown(Keys.Space);

            // Escape
            currentKeys[(int)GameKey.Escape] = Keyboard.GetState().IsKeyDown(Keys.Escape);
        }

        public bool IsKeyPressed(GameKey key)
        {
            return currentKeys[(int)key];
        }

        public bool IsKeyJustPressed(GameKey key)
        {
            return (currentKeys[(int)key] && !previousKeys[(int)key]);
        }

        public bool IsKeyJustPressed(char key)
        {
            return key >= 'A' && key <= 'Z' && currentLetters[key - 'A'] && !previousLetters[key - 'A'];
        }

        public bool IsKeyJustReleased(GameKey key)
        {
            return (!currentKeys[(int)key] && previousKeys[(int)key]);
        }
    }

}
